import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.csvreader.CsvReader;

public class TablesStatistics {
	private static TablesStatistics stat;
	private static Map<String, TableData> tableData = new HashMap<String, TableData>();;
	private static String STAT_FILE = "nc_table_stat.csv";
	
	private TablesStatistics() 	{	}
	
	public static TablesStatistics getStatistics ()
	{
		if (stat == null)
		{
			try {
				stat = new TablesStatistics();
				stat.initialize();
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
		return stat;
	}
	
	private static void initialize() throws NumberFormatException, IOException
	{
		CsvReader stat_file = new CsvReader(STAT_FILE);
		stat_file.readHeaders();
		while (stat_file.readRecord())
		{
			String name = stat_file.get("TABLE_NAME");
			int updates = new Integer(stat_file.get("UPDATES")).intValue();
			int inserts = new Integer(stat_file.get("INSERTS")).intValue();
			int deletes = new Integer(stat_file.get("DELETES")).intValue();
			TableData data = new TableData(name, updates, deletes, inserts);
			tableData.put(name, data);
		}
		
	}
	
	public Integer getTotalModifications(String tableName)
	{
		TableData data = tableData.get(tableName.toUpperCase());
		int total = 0;
		if(data!=null)
			total = data.getTotal();
		return new Integer(total);
	}
}
